﻿using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace brand.cyt369.com.dal
{
    public class CategoryService : brand.cyt369.com.dal.DbContexts.BrandBaseService<CategoryService>
    {
        /// <summary>
        /// 获取子类别 通过父级ID
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public IQueryable<brand.cyt369.com.models.Category> CategorysGetByParentID(int id)
        {
            var categorys = base.Get<brand.cyt369.com.models.Category>().Where(c => c.ParentID == id);
            return categorys.AsNoTracking();
        }

        /// <summary>
        /// 获取父级类别_包括子级
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public IQueryable<brand.cyt369.com.models.Category> ParentCategorysGets()
        {
            using (var ctx = this.BrandCtx)
            {
                var categorys = ctx.Categories.Include(c => c.Childs)
                    .Where(c => c.ParentID == 0)
                    .OrderBy(c => c.OrderNumber);

                return categorys.ToList().AsQueryable();
            }
        }

        /// <summary>
        /// 类别获取通过id
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public brand.cyt369.com.models.Category CategorysGetByID(int? id)
        {
            using (var ctx = this.BrandCtx)
            {
                var categorys = ctx.Categories
                    .Where(c => c.Id == id)
                    .FirstOrDefault();

                return categorys;
            }
        }

        /// <summary>
        /// 获取类别(包含所有子级) 通过id
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public brand.cyt369.com.models.Category CategorysGetByParentID_InclueChilds(int id)
        {
            using (var ctx = this.BrandCtx)
            {
                var categorys = ctx.Categories.Include(c=>c.Childs)
                    .Where(c => c.Id == id)
                    .FirstOrDefault();
                return categorys;
            }
        }
    }
}
